import java.awt.Color;

public class SuperTicTacToeGame 
{
	private SuperTicTacToeController controller; 
	
	private Board board;
	private Player[] players; 
	
	private int currPlayerIndex; 
	private SubGrid currSubGrid; 
	
	public SuperTicTacToeGame(SuperTicTacToeController c)
	{
		controller = c; 
		
		board = new Board();
		currSubGrid = board.get(1,1); 
		
		players = new Player[2]; 		
		players[0] = new HumanPlayer("Akshat", Color.BLUE, this); 
		players[1] = new HumanPlayer("Alfred", Color.RED, this); 
		currPlayerIndex = 0; 
	}
	
	public Board getBoard()
	{
		return board; 
	}
	
	public void playGame()
	{
		boolean won = false; 
		
		while( !won )
		{
			if( players[currPlayerIndex].play() )
			{
				currPlayerIndex++; 
				currPlayerIndex %= 2;
				won = board.checkForWinner(); 
			}
		
		}
	}
	
	
	public void moveMade(GridLocation loc)
	{
		if(players[currPlayerIndex] instanceof HumanPlayer)
		{
			if(currSubGrid == null || (loc.getMainRow() == currSubGrid.getMainRow() && loc.getMainCol() == currSubGrid.getMainCol()))
			{
				if( currSubGrid != null && currSubGrid.getOpenLocations().contains(loc) )
				{
					players[currPlayerIndex].setPlay(loc); 
				}
				else if( currSubGrid == null && board.get(loc.getMainRow(), loc.getMainCol()).getOpenLocations().contains(loc))
				{
					players[currPlayerIndex].setPlay(loc);
				}
				else
				{
					System.out.println("Invalid spot!"); 
					return; 
				}
			}
			else
			{
				System.out.println("Incorrect Grid!"); 
				return; 
			}
		}
		
		return; 
	}
	
	
	
}

